<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动播放音频演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        
        .container {
            text-align: center;
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            max-width: 600px;
            width: 90%;
        }
        
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        
        .controls {
            margin-top: 20px;
        }
        
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 0 10px;
        }
        
        button:hover {
            background-color: #45a049;
        }
        
        #status {
            margin-top: 20px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>自动播放音频演示</h1>
        <p>页面加载后将自动播放音频</p>
        <div class="controls">
            <button id="playBtn">播放</button>
            <button id="pauseBtn">暂停</button>
            <button id="stopBtn">停止</button>
        </div>
        <div id="status">准备播放...</div>
    </div>

    <!-- 音频元素 -->
    <audio id="audioPlayer" preload="auto">
        <source src="./audio/keyboard.mp3" type="audio/mpeg">
    </audio>

    <script>
        const audioPlayer = document.getElementById('audioPlayer');
        const playBtn = document.getElementById('playBtn');
        const pauseBtn = document.getElementById('pauseBtn');
        const stopBtn = document.getElementById('stopBtn');
        const statusEl = document.getElementById('status');

        // 自动播放函数
        function autoPlay() {
            // 现代浏览器要求用户交互后才能自动播放
            // 所以这里我们尝试自动播放，但可能会失败
            const playPromise = audioPlayer.play();
            
            if (playPromise !== undefined) {
                playPromise.then(_ => {
                    statusEl.textContent = "正在播放...";
                })
                .catch(error => {
                    statusEl.textContent = "自动播放被阻止，请点击播放按钮";
                });
            }
        }

        // 页面加载完成后尝试自动播放
        window.addEventListener('DOMContentLoaded', () => {
            // 设置音量
            audioPlayer.volume = 0.5;
            
            // 尝试自动播放
            autoPlay();
        });

        // 播放按钮事件
        playBtn.addEventListener('click', () => {
            audioPlayer.play();
            statusEl.textContent = "正在播放...";
        });

        // 暂停按钮事件
        pauseBtn.addEventListener('click', () => {
            audioPlayer.pause();
            statusEl.textContent = "已暂停";
        });

        // 停止按钮事件
        stopBtn.addEventListener('click', () => {
            audioPlayer.pause();
            audioPlayer.currentTime = 0;
            statusEl.textContent = "已停止";
        });

        // 音频结束事件
        audioPlayer.addEventListener('ended', () => {
            statusEl.textContent = "播放结束";
        });
    </script>
</body>
</html>
